home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK2.toast / Development Kits (Disc 2) / ScriptX / Draggable ScriptX Folders / utils / memory / memdiff.sx next >
Encoding:
Text File  |  1995-12-10  |  5.3 KB  |  210 lines  |  [TEXT/ttxt]

  1. --<<<
  2. /*
  3. What Memory Diff does
  4. ---------------------
  5. This tool allows you to see in some detail what changes have
  6. taken place in memory, by showing which classes have a different number
  7. of instances in memory.
  8.  
  9. You will get output from memoryDiff like
  10.  
  11.     There are 4 more instances of Rect.  There is a total of 14 instances.
  12.     There are 1 more instances of TwoDShape.  There are currently 18 instances.
  13.     There are 1 more instances of Region.  There are currently 23 instances.
  14.     There are 1 more instances of RamStream.  There are currently 2 instances.
  15.  
  16. You will usually also see reports of things related to the compiler, like
  17. expressionxlist, callxexpression, ApplyTree, etc.  These can usually be ignored.
  18. Also, there will always be at least 1 more instance of RamStream because
  19. MemoryDiff creates them.
  20.  
  21. Note that this tool only tells about what is going on in the ScriptX heap
  22. among ScriptX objects, it does not show what is happening in the "system" heap
  23. (where large data chunks like bitmap data and such are stored).
  24. To get the bigger picture of memory (including system heap), use a tool like moo.sx.
  25.  
  26. How to use the Memory Diff Tool
  27. -------------------------------
  28. This utility can be used in both 1.0 and 1.5
  29. First, you need to take a snap shot of memory.  To do this
  30.  
  31.     global m := memorySnap ()
  32.  
  33. This will remember the state of memory at that point in time.
  34. Then, at some later time when you want to see what has changed in 
  35. memory
  36.  
  37.     memoryDiff m
  38.  
  39. These calls are usually used paired together, but you can take several memorySnaps
  40. and then use memoryDiff with those snaps at different times.  
  41.  
  42. enjoy
  43. --jj
  44. */
  45.  
  46. function memorySnap ->
  47. (
  48.     garbageCollect()
  49.     
  50.     threadCriticalUp()
  51.         local d := debug
  52.         debug := new RamStream maxSize:30000
  53.         memoryUsage 1000
  54.         local t := debug
  55.         debug := d
  56.     threadCriticalDown()
  57.     
  58.     return t
  59. )
  60.  
  61. function getNextWord args -> (
  62.     if (findNthContext(args,@word)) then (
  63.         local returnStr := CopyFromTo(args[1],args[3],args[4])
  64.         args[3] := args[4] + 1
  65.         args[4] := 10000000 -- arbitrarily large number
  66.         return returnStr
  67.     )
  68.     else
  69.         return empty
  70. )
  71.  
  72. function memoryDiff memoryState ->
  73. (
  74.     garbageCollect()
  75.     
  76.     threadCriticalUp()
  77.         local d := debug
  78.         debug := new RamStream maxSize:30000
  79.         memoryUsage 1000
  80.         local t := debug
  81.         debug := d
  82.     threadCriticalDown()
  83.  
  84.     seekFromStart t 0
  85.     local len := readReady t
  86.     local afterSnap := new string
  87.     pipePartial afterSnap t len    
  88.  
  89.     local counter := 1
  90.     for i in afterSnap do
  91.     (
  92.         if ( i = 95 ) do
  93.             afterSnap[counter] := 120
  94.             
  95.         counter := counter + 1
  96.     )
  97.     print "finished xing the afterSnap"
  98.         
  99.     seekFromStart memoryState 0
  100.     len := readReady memoryState
  101.     local beforeSnap := new string
  102.     pipePartial beforeSnap memoryState len    
  103.  
  104.     counter := 1
  105.     for i in beforeSnap do
  106.     (
  107.         if ( i = 95 ) do
  108.             beforeSnap[counter] := 120
  109.             
  110.         counter := counter + 1
  111.     )
  112.     print "finished xing the beforeSnap"
  113.         
  114.     local objectKind
  115.     local numInstances
  116.     local beforeSnapList := new keyedlinkedList
  117.  
  118.     counter := 25
  119.     local args := #(beforeSnap,counter,0,1000000) as Quad
  120.     objectKind := (getNextWord(args)) as String
  121.     repeat while (objectKind != empty AND objectKind != "Found") do
  122.     (
  123.         args[2] := 1
  124.         numInstances := (getNextWord(args)) as integer
  125.         
  126.         add beforeSnapList objectKind numInstances
  127.  
  128.         args[2] := 3
  129.         objectKind := (getNextWord(args)) as String
  130.                 
  131.         if (objectKind = "struct") do
  132.         (
  133.             args[2] := 1
  134.             objectKind := (getNextWord(args)) as String
  135.         )
  136.  
  137.     )
  138.     print "finished parsing the beforeSnap"
  139.     
  140.     local oldNumber
  141.     local caption
  142.  
  143.     counter := 25
  144.     args := #(afterSnap,counter,0,1000000) as Quad
  145.     objectKind := (getNextWord(args)) as String
  146.     repeat while (objectKind != empty AND objectKind != "Found") do
  147.     (
  148.         args[2] := 1
  149.         numInstances := (getNextWord(args)) as integer
  150.             
  151.         oldNumber := beforeSnapList[objectKind]
  152.         
  153.         if (oldNumber = empty) then
  154.         (
  155.             caption := ("There are " + (numInstances as String) + " more instances of " \
  156.                         + objectKind + ".  There are currently " + (numInstances as String) + " instances.") as String
  157.             print caption
  158.         )
  159.         else if (oldNumber < numInstances) then
  160.         (
  161.             caption := ("There are " + ((numInstances - oldNumber) as String) + " more instances of "\
  162.                         + objectKind + ".  There are currently " + (numInstances as String) + " instances.") as String
  163.             print caption
  164.             
  165.             deleteKeyOne beforeSnapList objectKind
  166.         )
  167.         else if (oldNumber > numInstances) then
  168.         (
  169.             caption := ("There are " + ((oldNumber - numInstances) as String) + " fewer instances of " \
  170.                         + objectKind + ". There are currently " + (numInstances as String) + " instances.") as String
  171.             print caption           
  172.             
  173.             deleteKeyOne beforeSnapList objectKind
  174.         )
  175.         else
  176.              deleteKeyOne beforeSnapList objectKind
  177.  
  178.         args[2] := 3
  179.         objectKind := (getNextWord(args)) as String
  180.  
  181.         --print objectKind
  182.         
  183.         if (objectKind = "struct") do
  184.         (
  185.             args[2] := 1
  186.             objectKind := (getNextWord(args)) as String
  187.         )
  188.  
  189.     )
  190.  
  191.  
  192.     if (beforeSnapList.size > 0) do
  193.     (
  194.         foreachBinding  beforeSnapList ( key value ->
  195.             (
  196.                 caption := ("There are " + (value as String) + " fewer instances of " \
  197.                                 + key + ". There are currently 0 instances.") as String
  198.                 print caption            
  199.             ) ) undefined
  200.             
  201.         emptyout beforeSnapList
  202.     )
  203.     
  204.     print "**"
  205.     print "** use allInstances to get the actual instances of the mentioned classes. **"    
  206.     print "**"
  207. )
  208.     
  209. -->>>
  210.